home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Plotter / PlotController.m < prev    next >
Text File  |  1995-06-12  |  3KB  |  102 lines

  1.  
  2. /* PlotController.m -- Implementation file for the PlotController class */
  3.  
  4. #import "PlotController.h"
  5. #import "PlotView.h"
  6. #import <appkit/Application.h>
  7. #import <appkit/Listener.h>
  8. #import <appkit/Pasteboard.h>
  9. #import <appkit/Window.h>
  10. #import <appkit/ScrollView.h>
  11. #import <appkit/Text.h>
  12. #import <stdlib.h> 
  13. #import <string.h> 
  14. #include <mach.h>
  15.  
  16. @implementation PlotController
  17.  
  18. - appDidInit:sender 
  19. /*
  20.  * Responds to a message that's sent just before the application
  21.  * gets the first event.  PlotController initializes its inputText
  22.  * instance variable.  It makes itself the inputText's delegate and
  23.  * the services delegate for the application.  It also ensures that 
  24.  * the application's single window becomes the key window.
  25.  */
  26. {
  27.     inputText = [theScrollView docView];
  28.     [inputText setDelegate:self];
  29.     [[NXApp appListener] setServicesDelegate:self];
  30.     [[thePlotView window] makeKeyAndOrderFront:self]; 
  31.     return self;
  32. }
  33.  
  34. - textDidGetKeys:theText isEmpty:(BOOL)flag
  35. /*
  36.  * Responds to a message the Text object sends when its text changes.
  37.  * PlotController causes the PlotView to clear itself when the text changes.
  38.  */
  39. {
  40.     return [thePlotView clear:self];
  41. }
  42.  
  43. - requestPlot:(id)pasteboard userData:(const char *)userData error: (char **)msg
  44. /*
  45.  * Responds to a request from another application for the plotting service.
  46.  * PlotController copies the data from the supplied pasteboard into the Text object
  47.  * and then sends a plot: message to the PlotView.
  48.  */
  49. {
  50.     char    *data, *scratch;
  51.     int     length;
  52.  
  53.     [NXApp activateSelf:NO]; 
  54.     [pasteboard types];
  55.     if ([pasteboard readType:NXAsciiPboardType data:&data length:&length]) {
  56.         if(scratch = malloc(length+1)) {
  57.             strncpy(scratch, data, length);
  58.             scratch[length]='\0';
  59.             [[inputText selectAll:self] replaceSel:scratch];
  60.             free(scratch);
  61.         }
  62.         vm_deallocate(task_self(), (vm_address_t)data, (vm_size_t)length);
  63.         [thePlotView plot:self];
  64.     }
  65.     return self;
  66. }
  67.  
  68.  
  69. - plotView:sender providePoints:(NXStream **)stream
  70. /*
  71.  * Responds to a message the PlotView sends requesting the points to plot.  
  72.  * PlotController responds by giving the PlotView access to the Text 
  73.  * object's stream.
  74.  */
  75. {
  76.     *stream = [inputText stream];
  77.     return self;
  78. }
  79.  
  80. - plotView:sender pointDidChange:(NXPoint *)aPoint
  81. /*
  82.  * Responds to a message the PlotView sends notifying its delegate 
  83.  * that a point has been added.  PlotController responds by adding the point to
  84.  * the end of the list in the Text object.
  85.  */
  86. {
  87.     int    endPos;
  88.     char    buffer[100];
  89.  
  90.     sprintf(buffer, "%d, %d\n", (int)aPoint->x, (int)aPoint->y);
  91.     endPos = [inputText byteLength];
  92.     [inputText setSel:endPos :endPos];
  93.     [inputText scrollSelToVisible];
  94.     [inputText replaceSel:buffer];
  95.     return self;
  96. }
  97.  
  98. @end
  99.  
  100.  
  101.  
  102.